
<?php
	include("gen.php");
	$cmd=get_datan("cmd");
	switch($cmd){
		case 1:
			//get one vaccine based on id
			get_user();
			break;
		case 2:
			//get all vaccine records and return as array
			//generate a json array/object/notation that represents all records in the vaccine table
			get_all_users();
			break;
		case 3:
			//update_vaccine($_REQUEST['vn'],$_REQUEST['vs'],$_REQUEST['vu']);
			//update_vaccine(get_data('vn'),get_datan('vs'),get_data('url'));
			update_user();		
			break;
		case 4:
			add_user();
			break;
		case 5:
			search_user();
			break;
			//get the data from url/request and update the vaccine record
		default:
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","unknown command");
			echo "}";
	}
	
	
	function get_user(){
		include_once("users.php");
		
		$id=get_datan("id");
		$u=new users();
		$res=$u->get_user($id);
		$row = $u->fetch($res);
		if(!$row){
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","user not found");
			echo "}";
			return;
		}
		
			//echo $row['firstname'];
		echo "{";
			echo jsonn("result",1) .",";
			echo '"user":{';
			echo jsonn("id",$id) .",";
			//first name
			echo jsons("firstname",$row['firstname']) .",";

			//last name
			echo jsons("lastname",$row['lastname']) .",";
			//username
			echo jsons("user_name",$row['user_name']) .",";
			//password
			echo jsons("password",$row['password']) .",";
			//position
			echo jsonn("position",$row['position']) .",";
			//status
			echo jsonn("status",$row['status']) .",";
			//date_created
			echo jsons("date_created",$row['date_created']) .",";
			//phone_number
			echo jsonn("phone_number",$row['phone_number']);	
			echo "}";
		echo "}";
	
	}

	function get_all_users(){ 
		include_once("users.php");
		$u= new users();
		$u->get_all_users();
		$row =$u->fetch();  
		if(!$row){
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","user not found");
			echo "}";
			return;
		}
		echo "{";
		echo jsonn("result",1) .",";
		echo '"users":';
		$val = Array(); 
		
		do { 
			array_push($val, $row);
			$row =$u->fetch();   
		}while ($row);	 
		print_r(json_encode($val));
		echo "}";
	}


	function update_user(){
		$id=get_datan('id');
		$firstname=get_data('fn');
		$lastname=get_data('ln');
		$user_name=get_data('un');
		$password=get_data('pw');
		$position=get_datan('ps');
		$status=get_datan('st');
		$date_created=get_data('dc');
		$phone_number=get_datan('pn');
		if (!$id){
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","invalid id");
			echo "}";
			return;
		}
		include ("users.php");
		$u= new users();
		$valid=$u->update_users($id,$firstname,$lastname,$user_name,$password,$position,$status,$date_created,$phone_number);
		if($valid){
			echo "{";
			echo jsonn("result",1). ",";
			echo jsons("message","updated");
			echo "}";
		}else{
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","update failed");
			echo "}";
		}


	}

	function add_user(){
		//$id=get_datan('id');
		$firstname=get_data('fn');
		$lastname=get_data('ln');
		$user_name=get_data('un');
		$password=get_data('pw');
		$position=get_datan('ps');
		$status=get_datan('st');
		$date_created=get_data('dc');
		$phone_number=get_datan('pn');
		include ("users.php");
		$u= new users();
		$valid=$u->add_users($firstname,$lastname,$user_name,$password,$position,$status,$date_created,$phone_number);
		if($valid){
			echo "{";
			echo jsonn("result",1). ",";
			echo jsons("message","added");
			echo "}";
		}else{
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","insertion failed");
			echo "}";
		}
// http://localhost/webtech/final3/user_action.php?cmd=4&fn=Lenovo&ln=Cjar&un=len.cj&pw=dD&ps=4&st=1&dc=&pn=244622393

	}

	function search_user(){
		//$id=get_datan('id');
		$firstname=get_data('fn');
		if ($firstname == null){
			return get_all_users();

		}
		
		include ("users.php");
		$u= new users();
		$valid=$u->search($firstname);
		if (!$valid){
		echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","user not found");
			echo "}";
			return;	
		}
		$row=$u->fetch();



		if(!$row){
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","user not found");
			echo "}";
			return;
			}

			
	
		else{
			echo "{";
			echo jsonn("result",1). ",";
			echo '"users":';

		$val = Array(); 	
		do { 
			array_push($val, $row);
			$row =$u->fetch();   
		}while ($row);	 
		print_r(json_encode($val));
		echo "}";
	}
		


	}

?>